package com.wmh.baseservice.operationaid.controller.details;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONWriter;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.operationaid.entity.ProductDetails;
import com.wmh.baseservice.operationaid.pojo.vo.details.ProductDetailsListReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.details.ProductDetailsPageReqVO;
import com.wmh.baseservice.operationaid.service.ProductDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

@Validated
@RestController
@RequestMapping("operationAid/details")
@Api(tags = "商品数据-商品明细")
public class ProductDetailsController {
    @Resource
    private ProductDetailsService productDetailsService;


    @ApiOperation("获取商品明细店铺选项列表")
    @GetMapping("storeOptions")
    public String getStoreOptions(){
        return success(productDetailsService.getStoreOptions());
    }

    @ApiOperation("获取商品明细数据周期选项列表")
    @GetMapping("dataCycleList")
    public String getDataCycleOptions(){
        return success(productDetailsService.getDataCycleOptions());
    }

    @ApiOperation(value = "获取商品明细列表")
    @GetMapping("/list")
    public String list(ProductDetailsListReqVO reqVO) {
        return success(productDetailsService.getProductDetailsList(reqVO), JSONWriter.Feature.NullAsDefaultValue);
    }

    @ApiOperation(value = "获取商品明细分页列表")
    @GetMapping("/page")
    public String page(@Validated ProductDetailsPageReqVO reqVO){

        PageResult<ProductDetails> pageResult = new PageUtil<ProductDetails>(reqVO).startPage((page, wrapper) -> {

            wrapper.eq(StrUtil.isNotBlank(reqVO.getStoreName()), ProductDetails.COL_STORE_NAME , reqVO.getStoreName());
            wrapper.eq(StrUtil.isNotBlank(reqVO.getDataCycle()), ProductDetails.COL_DATA_CYCLE, reqVO.getDataCycle());
            wrapper.eq(reqVO.getProductId() != null, ProductDetails.COL_PRODUCT_ID , reqVO.getProductId());
            productDetailsService.page(page, wrapper);
        });
        return success(pageResult);
    }

    @ApiOperation("获取商品明细店铺选项列表")
    @PostMapping("/download")
    public void downloadExcel(HttpServletResponse response, @Validated ProductDetailsPageReqVO reqVO) {
        productDetailsService.writeExcel(response, reqVO);
    }

}
